home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / perlman / man / perlre.txt < prev    next >
Encoding:
Text File  |  1999-09-09  |  24.7 KB  |  578 lines

  1. NAME
  2.        perlre - Perl regular expressions
  3.  
  4. DESCRIPTION
  5.        This page describes the syntax of regular expressions in
  6.        Perl.  For a description of how to actually use regular
  7.        expressions in matching operations, plus various examples
  8.        of the same, see m// and s/// in the perlop manpage.
  9.  
  10.        The matching operations can have various modifiers, some
  11.        of which relate to the interpretation of the regular
  12.        expression inside.  These are:
  13.  
  14.            i   Do case-insensitive pattern matching.
  15.            m   Treat string as multiple lines.
  16.            s   Treat string as single line.
  17.            x   Extend your pattern's legibility with whitespace and comments.
  18.  
  19.        These are usually written as "the /x modifier", even
  20.        though the delimiter in question might not actually be a
  21.        slash.  In fact, any of these modifiers may also be
  22.        embedded within the regular expression itself using the
  23.        new (?...) construct.  See below.
  24.  
  25.        The /x modifier itself needs a little more explanation.
  26.        It tells the regular expression parser to ignore
  27.        whitespace that is not backslashed or within a character
  28.        class.  You can use this to break up your regular
  29.        expression into (slightly) more readable parts.  The #
  30.        character is also treated as a metacharacter introducing a
  31.        comment, just as in ordinary Perl code.  Taken together,
  32.        these features go a long way towards making Perl 5 a
  33.        readable language.  See the C comment deletion code in the
  34.        perlop manpage.
  35.  
  36.        Regular Expressions
  37.  
  38.        The patterns used in pattern matching are regular
  39.        expressions such as those supplied in the Version 8 regexp
  40.        routines.  (In fact, the routines are derived (distantly)
  41.        from Henry Spencer's freely redistributable
  42.        reimplementation of the V8 routines.)  See the section on
  43.        Version 8 Regular Expressions for details.
  44.  
  45.        In particular the following metacharacters have their
  46.        standard egrep-ish meanings:
  47.  
  48.            \   Quote the next metacharacter
  49.            ^   Match the beginning of the line
  50.            .   Match any character (except newline)
  51.            $   Match the end of the line (or before newline at the end)
  52.            |   Alternation
  53.            ()  Grouping
  54.            []  Character class
  55.        By default, the "^" character is guaranteed to match only
  56.        at the beginning of the string, the "$" character only at
  57.        the end (or before the newline at the end) and Perl does
  58.        certain optimizations with the assumption that the string
  59.        contains only one line.  Embedded newlines will not be
  60.        matched by "^" or "$".  You may, however, wish to treat a
  61.        string as a multi-line buffer, such that the "^" will
  62.        match after any newline within the string, and "$" will
  63.        match before any newline.  At the cost of a little more
  64.        overhead, you can do this by using the /m modifier on the
  65.        pattern match operator.  (Older programs did this by
  66.        setting $*, but this practice is deprecated in Perl 5.)
  67.  
  68.        To facilitate multi-line substitutions, the "." character
  69.        never matches a newline unless you use the /s modifier,
  70.        which tells Perl to pretend the string is a single
  71.        line--even if it isn't.  The /s modifier also overrides
  72.        the setting of $*, in case you have some (badly behaved)
  73.        older code that sets it in another module.
  74.  
  75.        The following standard quantifiers are recognized:
  76.  
  77.            *      Match 0 or more times
  78.            +      Match 1 or more times
  79.            ?      Match 1 or 0 times
  80.            {n}    Match exactly n times
  81.            {n,}   Match at least n times
  82.            {n,m}  Match at least n but not more than m times
  83.  
  84.        (If a curly bracket occurs in any other context, it is
  85.        treated as a regular character.)  The "*" modifier is
  86.        equivalent to {0,}, the "+" modifier to {1,}, and the "?"
  87.        modifier to {0,1}.  n and m are limited to integral values
  88.        less than 65536.
  89.  
  90.        By default, a quantified subpattern is "greedy", that is,
  91.        it will match as many times as possible without causing
  92.        the rest of the pattern not to match.  The standard
  93.        quantifiers are all "greedy", in that they match as many
  94.        occurrences as possible (given a particular starting
  95.        location) without causing the pattern to fail.  If you
  96.        want it to match the minimum number of times possible,
  97.        follow the quantifier with a "?" after any of them.  Note
  98.        that the meanings don't change, just the "gravity":
  99.  
  100.            *?     Match 0 or more times
  101.            +?     Match 1 or more times
  102.            ??     Match 0 or 1 time
  103.            {n}?   Match exactly n times
  104.            {n,}?  Match at least n times
  105.            {n,m}? Match at least n but not more than m times
  106.  
  107.        Since patterns are processed as double quoted strings, the
  108.        following also work:
  109.            \t          tab
  110.            \n          newline
  111.            \r          return
  112.            \f          form feed
  113.            \a          alarm (bell)
  114.            \e          escape (think troff)
  115.            \033        octal char (think of a PDP-11)
  116.            \x1B        hex char
  117.            \c[         control char
  118.            \l          lowercase next char (think vi)
  119.            \u          uppercase next char (think vi)
  120.            \L          lowercase till \E (think vi)
  121.            \U          uppercase till \E (think vi)
  122.            \E          end case modification (think vi)
  123.            \Q          quote regexp metacharacters till \E
  124.  
  125.        In addition, Perl defines the following:
  126.  
  127.            \w  Match a "word" character (alphanumeric plus "_")
  128.            \W  Match a non-word character
  129.            \s  Match a whitespace character
  130.            \S  Match a non-whitespace character
  131.            \d  Match a digit character
  132.            \D  Match a non-digit character
  133.  
  134.        Note that \w matches a single alphanumeric character, not
  135.        a whole word.  To match a word you'd need to say \w+.  You
  136.        may use \w, \W, \s, \S, \d and \D within character classes
  137.        (though not as either end of a range).
  138.  
  139.        Perl defines the following zero-width assertions:
  140.  
  141.            \b  Match a word boundary
  142.            \B  Match a non-(word boundary)
  143.            \A  Match only at beginning of string
  144.            \Z  Match only at end of string (or before newline at the end)
  145.            \G  Match only where previous m//g left off
  146.  
  147.        A word boundary (\b) is defined as a spot between two
  148.        characters that has a \w on one side of it and and a \W on
  149.        the other side of it (in either order), counting the
  150.        imaginary characters off the beginning and end of the
  151.        string as matching a \W.  (Within character classes \b
  152.        represents backspace rather than a word boundary.)  The \A
  153.        and \Z are just like "^" and "$" except that they won't
  154.        match multiple times when the /m modifier is used, while
  155.        "^" and "$" will match at every internal line boundary.
  156.        To match the actual end of the string, not ignoring
  157.        newline, you can use \Z(?!\n).
  158.  
  159.        When the bracketing construct ( ... ) is used, \<digit>
  160.        matches the digit'th substring.  Outside of the pattern,
  161.        always use "$" instead of "\" in front of the digit.
  162.        (While the \<digit> notation can on rare occasion work
  163.        outside the current pattern, this should not be relied
  164.        upon.  See the WARNING below.) The scope of $<digit> (and
  165.        $`, $&, and $') extends to the end of the enclosing BLOCK
  166.        or eval string, or to the next successful pattern match,
  167.        whichever comes first.  If you want to use parentheses to
  168.        delimit a subpattern (e.g. a set of alternatives) without
  169.        saving it as a subpattern, follow the ( with a ?.
  170.  
  171.        You may have as many parentheses as you wish.  If you have
  172.        more than 9 substrings, the variables $10, $11, ... refer
  173.        to the corresponding substring.  Within the pattern, \10,
  174.        \11, etc. refer back to substrings if there have been at
  175.        least that many left parens before the backreference.
  176.        Otherwise (for backward compatibility) \10 is the same as
  177.        \010, a backspace, and \11 the same as \011, a tab.  And
  178.        so on.  (\1 through \9 are always backreferences.)
  179.  
  180.        $+ returns whatever the last bracket match matched.  $&
  181.        returns the entire matched string.  ($0 used to return the
  182.        same thing, but not any more.)  $` returns everything
  183.        before the matched string.  $' returns everything after
  184.        the matched string.  Examples:
  185.  
  186.            s/^([^ ]*) *([^ ]*)/$2 $1/;     # swap first two words
  187.  
  188.            if (/Time: (..):(..):(..)/) {
  189.                $hours = $1;
  190.                $minutes = $2;
  191.                $seconds = $3;
  192.            }
  193.  
  194.        You will note that all backslashed metacharacters in Perl
  195.        are alphanumeric, such as \b, \w, \n.  Unlike some other
  196.        regular expression languages, there are no backslashed
  197.        symbols that aren't alphanumeric.  So anything that looks
  198.        like \\, \(, \), \<, \>, \{, or \} is always interpreted
  199.        as a literal character, not a metacharacter.  This makes
  200.        it simple to quote a string that you want to use for a
  201.        pattern but that you are afraid might contain
  202.        metacharacters.  Simply quote all the non-alphanumeric
  203.        characters:
  204.  
  205.            $pattern =~ s/(\W)/\\$1/g;
  206.  
  207.        You can also use the built-in quotemeta() function to do
  208.        this.  An even easier way to quote metacharacters right in
  209.        the match operator is to say
  210.  
  211.            /$unquoted\Q$quoted\E$unquoted/
  212.  
  213.        Perl 5 defines a consistent extension syntax for regular
  214.        expressions.  The syntax is a pair of parens with a
  215.        question mark as the first thing within the parens (this
  216.        was a syntax error in Perl 4).  The character after the
  217.        question mark gives the function of the extension.
  218.        Several extensions are already supported:
  219.  
  220.        (?#text)  A comment.  The text is ignored.  If the /x
  221.                  switch is used to enable whitespace formatting,
  222.                  a simple # will suffice.
  223.  
  224.        (?:regexp)
  225.                  This groups things like "()" but doesn't make
  226.                  backrefences like "()" does.  So
  227.  
  228.                      split(/\b(?:a|b|c)\b/)
  229.  
  230.                  is like
  231.  
  232.                      split(/\b(a|b|c)\b/)
  233.  
  234.                  but doesn't spit out extra fields.
  235.  
  236.        (?=regexp)
  237.                  A zero-width positive lookahead assertion.  For
  238.                  example, /\w+(?=\t)/ matches a word followed by
  239.                  a tab, without including the tab in $&.
  240.  
  241.        (?!regexp)
  242.                  A zero-width negative lookahead assertion.  For
  243.                  example /foo(?!bar)/ matches any occurrence of
  244.                  "foo" that isn't followed by "bar".  Note
  245.                  however that lookahead and lookbehind are NOT
  246.                  the same thing.  You cannot use this for
  247.                  lookbehind: /(?!foo)bar/ will not find an
  248.                  occurrence of "bar" that is preceded by
  249.                  something which is not "foo".  That's because
  250.                  the (?!foo) is just saying that the next thing
  251.                  cannot be "foo"--and it's not, it's a "bar", so
  252.                  "foobar" will match.  You would have to do
  253.                  something like /(?foo)...bar/ for that.   We say
  254.                  "like" because there's the case of your "bar"
  255.                  not having three characters before it.  You
  256.                  could cover that this way:
  257.                  /(?:(?!foo)...|^..?)bar/.  Sometimes it's still
  258.                  easier just to say:
  259.  
  260.                      if (/foo/ && $` =~ /bar$/)
  261.  
  262.        (?imsx)   One or more embedded pattern-match modifiers.
  263.                  This is particularly useful for patterns that
  264.                  are specified in a table somewhere, some of
  265.                  which want to be case sensitive, and some of
  266.                  which don't.  The case insensitive ones merely
  267.                  need to include (?i) at the front of the
  268.                  pattern.  For example:
  269.  
  270.                      $pattern = "foobar";
  271.                      if ( /$pattern/i )
  272.  
  273.                      # more flexible:
  274.  
  275.                      $pattern = "(?i)foobar";
  276.                      if ( /$pattern/ )
  277.  
  278.        The specific choice of question mark for this and the new
  279.        minimal matching construct was because 1) question mark is
  280.        pretty rare in older regular expressions, and 2) whenever
  281.        you see one, you should stop and "question" exactly what
  282.        is going on.  That's psychology...
  283.  
  284.        Backtracking
  285.  
  286.        A fundamental feature of regular expression matching
  287.        involves the notion called backtracking.  which is used
  288.        (when needed) by all regular expression quantifiers,
  289.        namely *, *?, +, +?, {n,m}, and {n,m}?.
  290.  
  291.        For a regular expression to match, the entire regular
  292.        expression must match, not just part of it.  So if the
  293.        beginning of a pattern containing a quantifier succeeds in
  294.        a way that causes later parts in the pattern to fail, the
  295.        matching engine backs up and recalculates the beginning
  296.        part--that's why it's called backtracking.
  297.  
  298.        Here is an example of backtracking:  Let's say you want to
  299.        find the word following "foo" in the string "Food is on
  300.        the foo table.":
  301.  
  302.            $_ = "Food is on the foo table.";
  303.            if ( /\b(foo)\s+(\w+)/i ) {
  304.                print "$2 follows $1.\n";
  305.            }
  306.  
  307.        When the match runs, the first part of the regular
  308.        expression (\b(foo)) finds a possible match right at the
  309.        beginning of the string, and loads up $1 with "Foo".
  310.        However, as soon as the matching engine sees that there's
  311.        no whitespace following the "Foo" that it had saved in $1,
  312.        it realizes its mistake and starts over again one
  313.        character after where it had had the tentative match.
  314.        This time it goes all the way until the next occurrence of
  315.        "foo". The complete regular expression matches this time,
  316.        and you get the expected output of "table follows foo."
  317.  
  318.        Sometimes minimal matching can help a lot.  Imagine you'd
  319.        like to match everything between "foo" and "bar".
  320.        Initially, you write something like this:
  321.  
  322.            $_ =  "The food is under the bar in the barn.";
  323.            if ( /foo(.*)bar/ ) {
  324.                print "got <$1>\n";
  325.            }
  326.  
  327.        Which perhaps unexpectedly yields:
  328.  
  329.          got <d is under the bar in the >
  330.  
  331.        That's because .* was greedy, so you get everything
  332.        between the first "foo" and the last "bar".  In this case,
  333.        it's more effective to use minimal matching to make sure
  334.        you get the text between a "foo" and the first "bar"
  335.        thereafter.
  336.  
  337.            if ( /foo(.*?)bar/ ) { print "got <$1>\n" }
  338.          got <d is under the >
  339.  
  340.        Here's another example: let's say you'd like to match a
  341.        number at the end of a string, and you also want to keep
  342.        the preceding part the match.  So you write this:
  343.  
  344.            $_ = "I have 2 numbers: 53147";
  345.            if ( /(.*)(\d*)/ ) {                                # Wrong!
  346.                print "Beginning is <$1>, number is <$2>.\n";
  347.            }
  348.  
  349.        That won't work at all, because .* was greedy and gobbled
  350.        up the whole string. As \d* can match on an empty string
  351.        the complete regular expression matched successfully.
  352.  
  353.            Beginning is <I have 2: 53147>, number is <>.
  354.  
  355.        Here are some variants, most of which don't work:
  356.  
  357.            $_ = "I have 2 numbers: 53147";
  358.            @pats = qw{
  359.                (.*)(\d*)
  360.                (.*)(\d+)
  361.                (.*?)(\d*)
  362.                (.*?)(\d+)
  363.                (.*)(\d+)$
  364.                (.*?)(\d+)$
  365.                (.*)\b(\d+)$
  366.                (.*\D)(\d+)$
  367.            };
  368.  
  369.  
  370.  
  371.  
  372.  
  373.            for $pat (@pats) {
  374.                printf "%-12s ", $pat;
  375.                if ( /$pat/ ) {
  376.                    print "<$1> <$2>\n";
  377.                } else {
  378.                    print "FAIL\n";
  379.                }
  380.            }
  381.  
  382.        That will print out:
  383.  
  384.            (.*)(\d*)    <I have 2 numbers: 53147> <>
  385.            (.*)(\d+)    <I have 2 numbers: 5314> <7>
  386.            (.*?)(\d*)   <> <>
  387.            (.*?)(\d+)   <I have > <2>
  388.            (.*)(\d+)$   <I have 2 numbers: 5314> <7>
  389.            (.*?)(\d+)$  <I have 2 numbers: > <53147>
  390.            (.*)\b(\d+)$ <I have 2 numbers: > <53147>
  391.            (.*\D)(\d+)$ <I have 2 numbers: > <53147>
  392.  
  393.        As you see, this can be a bit tricky.  It's important to
  394.        realize that a regular expression is merely a set of
  395.        assertions that gives a definition of success.  There may
  396.        be 0, 1, or several different ways that the definition
  397.        might succeed against a particular string.  And if there
  398.        are multiple ways it might succeed, you need to understand
  399.        backtracking in order to know which variety of success you
  400.        will achieve.
  401.  
  402.        When using lookahead assertions and negations, this can
  403.        all get even tricker.  Imagine you'd like to find a
  404.        sequence of nondigits not followed by "123".  You might
  405.        try to write that as
  406.  
  407.                $_ = "ABC123";
  408.                if ( /^\D*(?!123)/ ) {                          # Wrong!
  409.                    print "Yup, no 123 in $_\n";
  410.                }
  411.  
  412.        But that isn't going to match; at least, not the way
  413.        you're hoping.  It claims that there is no 123 in the
  414.        string.  Here's a clearer picture of why it that pattern
  415.        matches, contrary to popular expectations:
  416.  
  417.            $x = 'ABC123' ;
  418.            $y = 'ABC445' ;
  419.  
  420.            print "1: got $1\n" if $x =~ /^(ABC)(?!123)/ ;
  421.            print "2: got $1\n" if $y =~ /^(ABC)(?!123)/ ;
  422.  
  423.            print "3: got $1\n" if $x =~ /^(\D*)(?!123)/ ;
  424.            print "4: got $1\n" if $y =~ /^(\D*)(?!123)/ ;
  425.  
  426.        This prints
  427.            2: got ABC
  428.            3: got AB
  429.            4: got ABC
  430.  
  431.        You might have expected test 3 to fail because it just
  432.        seems to a more general purpose version of test 1.  The
  433.        important difference between them is that test 3 contains
  434.        a quantifier (\D*) and so can use backtracking, whereas
  435.        test 1 will not.  What's happening is that you've asked
  436.        "Is it true that at the start of $x, following 0 or more
  437.        nondigits, you have something that's not 123?"  If the
  438.        pattern matcher had let \D* expand to "ABC", this would
  439.        have caused the whole pattern to fail.  The search engine
  440.        will initially match \D* with "ABC".  Then it will try to
  441.        match (?!123 with "123" which, of course, fails.  But
  442.        because a quantifier (\D*) has been used in the regular
  443.        expression, the search engine can backtrack and retry the
  444.        match differently in the hope of matching the complete
  445.        regular expression.
  446.  
  447.        Well now, the pattern really, really wants to succeed, so
  448.        it uses the standard regexp backoff-and-retry and lets \D*
  449.        expand to just "AB" this time.  Now there's indeed
  450.        something following "AB" that is not "123".  It's in fact
  451.        "C123", which suffices.
  452.  
  453.        We can deal with this by using both an assertion and a
  454.        negation.  We'll say that the first part in $1 must be
  455.        followed by a digit, and in fact, it must also be followed
  456.        by something that's not "123".  Remember that the
  457.        lookaheads are zero-width expressions--they only look, but
  458.        don't consume any of the string in their match.  So
  459.        rewriting this way produces what you'd expect; that is,
  460.        case 5 will fail, but case 6 succeeds:
  461.  
  462.            print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/ ;
  463.            print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/ ;
  464.  
  465.            6: got ABC
  466.  
  467.        In other words, the two zero-width assertions next to each
  468.        other work like they're ANDed together, just as you'd use
  469.        any builtin assertions:  /^$/ matches only if you're at
  470.        the beginning of the line AND the end of the line
  471.        simultaneously.  The deeper underlying truth is that
  472.        juxtaposition in regular expressions always means AND,
  473.        except when you write an explicit OR using the vertical
  474.        bar.  /ab/ means match "a" AND (then) match "b", although
  475.        the attempted matches are made at different positions
  476.        because "a" is not a zero-width assertion, but a one-width
  477.        assertion.
  478.  
  479.        One warning: particularly complicated regular expressions
  480.        can take exponential time to solve due to the immense
  481.        number of possible ways they can use backtracking to try
  482.        match.  For example this will take a very long time to run
  483.  
  484.            /((a{0,5}){0,5}){0,5}/
  485.  
  486.        And if you used *'s instead of limiting it to 0 through 5
  487.        matches, then it would take literally forever--or until
  488.        you ran out of stack space.
  489.  
  490.        Version 8 Regular Expressions
  491.  
  492.        In case you're not familiar with the "regular" Version 8
  493.        regexp routines, here are the pattern-matching rules not
  494.        described above.
  495.  
  496.        Any single character matches itself, unless it is a
  497.        metacharacter with a special meaning described here or
  498.        above.  You can cause characters which normally function
  499.        as metacharacters to be interpreted literally by prefixing
  500.        them with a "\" (e.g. "\." matches a ".", not any
  501.        character; "\\" matches a "\").  A series of characters
  502.        matches that series of characters in the target string, so
  503.        the pattern blurfl would match "blurfl" in the target
  504.        string.
  505.  
  506.        You can specify a character class, by enclosing a list of
  507.        characters in [], which will match any one of the
  508.        characters in the list.  If the first character after the
  509.        "[" is "^", the class matches any character not in the
  510.        list.  Within a list, the "-" character is used to specify
  511.        a range, so that a-z represents all the characters between
  512.        "a" and "z", inclusive.
  513.  
  514.        Characters may be specified using a metacharacter syntax
  515.        much like that used in C: "\n" matches a newline, "\t" a
  516.        tab, "\r" a carriage return, "\f" a form feed, etc.  More
  517.        generally, \nnn, where nnn is a string of octal digits,
  518.        matches the character whose ASCII value is nnn.
  519.        Similarly, \xnn, where nn are hexidecimal digits, matches
  520.        the character whose ASCII value is nn. The expression \cx
  521.        matches the ASCII character control-x.  Finally, the "."
  522.        metacharacter matches any character except "\n" (unless
  523.        you use /s).
  524.  
  525.        You can specify a series of alternatives for a pattern
  526.        using "|" to separate them, so that fee|fie|foe will match
  527.        any of "fee", "fie", or "foe" in the target string (as
  528.        would f(e|i|o)e).  Note that the first alternative
  529.        includes everything from the last pattern delimiter ("(",
  530.        "[", or the beginning of the pattern) up to the first "|",
  531.        and the last alternative contains everything from the last
  532.        "|" to the next pattern delimiter.  For this reason, it's
  533.        common practice to include alternatives in parentheses, to
  534.        minimize confusion about where they start and end.  Note
  535.        however that "|" is interpreted as a literal with square
  536.        brackets, so if you write [fee|fie|foe] you're really only
  537.        matching [feio|].
  538.  
  539.        Within a pattern, you may designate subpatterns for later
  540.        reference by enclosing them in parentheses, and you may
  541.        refer back to the nth subpattern later in the pattern
  542.        using the metacharacter \n.  Subpatterns are numbered
  543.        based on the left to right order of their opening
  544.        parenthesis.  Note that a backreference matches whatever
  545.        actually matched the subpattern in the string being
  546.        examined, not the rules for that subpattern.  Therefore,
  547.        (0|0x)\d*\s\1\d* will match "0x1234 0x4321",but not
  548.        "0x1234 01234", since subpattern 1 actually matched "0x",
  549.        even though the rule 0|0x could potentially match the
  550.        leading 0 in the second number.
  551.  
  552.        WARNING on \1 vs $1
  553.  
  554.        Some people get too used to writing things like
  555.  
  556.            $pattern =~ s/(\W)/\\\1/g;
  557.  
  558.        This is grandfathered for the RHS of a substitute to avoid
  559.        shocking the sed addicts, but it's a dirty habit to get
  560.        into.  That's because in PerlThink, the right-hand side of
  561.        a s/// is a double-quoted string.  \1 in the usual double-
  562.        quoted string means a control-A.  The customary Unix
  563.        meaning of \1 is kludged in for s///.  However, if you get
  564.        into the habit of doing that, you get yourself into
  565.        trouble if you then add an /e modifier.
  566.  
  567.            s/(\d+)/ \1 + 1 /eg;
  568.  
  569.        Or if you try to do
  570.  
  571.            s/(\d+)/\1000/;
  572.  
  573.        You can't disambiguate that by saying \{1}000, whereas you
  574.        can fix it with ${1}000.  Basically, the operation of
  575.        interpolation should not be confused with the operation of
  576.        matching a backreference.  Certainly they mean two
  577.        different things on the left side of the s///.
  578.